package stream.math;

import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Client {
    public static void main(String[] args) {
        List<Sys_testClass> listTest = new ArrayList<>();

        Sys_testClass test1 = new Sys_testClass();
        test1.setName("商品1");
        test1.setPrice(3200.0);
        test1.setNumber(3);
        test1.setSum_price(test1.getPrice() * test1.getNumber());
        listTest.add(test1);

        Sys_testClass test2 = new Sys_testClass();
        test2.setName("商品2");
        test2.setPrice(1599.0);
        test2.setNumber(6);
        test2.setSum_price(test2.getPrice() * test2.getNumber());
        listTest.add(test2);

        Sys_testClass test3 = new Sys_testClass();
        test3.setName("商品3");
        test3.setPrice(999.0);
        test3.setNumber(2);
        test3.setSum_price(test3.getPrice() * test3.getNumber());
        listTest.add(test3);

        double sum_price = listTest.stream().mapToDouble(Sys_testClass::getSum_price).sum();

        test1();
    }

    public static void test1() {
        InfoPubParam param1 = new InfoPubParam();
        param1.setIsbn("1234");
        param1.setAmount(5L);

        InfoPubParam param2 = new InfoPubParam();
        param2.setIsbn("123434");
        param2.setAmount(2L);

        InfoPubParam param3 = new InfoPubParam();
        param3.setIsbn("156784");
        param3.setAmount(4L);


        InfoPubParam param4 = new InfoPubParam();
        param4.setIsbn("1234");
        param4.setAmount(3L);

        InfoPubParam param5 = new InfoPubParam();
        param5.setIsbn("1234");
        param5.setAmount(1L);

        List<InfoPubParam> paramList = new ArrayList<>();
        paramList.add(param1);
        paramList.add(param2);
        paramList.add(param3);
        paramList.add(param4);
        paramList.add(param5);

        List<InfoPubParam> paramList1 = new ArrayList<>();
        Map<String, InfoPubParam> infoPubParamMap = paramList.stream().collect(Collectors.toMap(InfoPubParam::getIsbn, Function.identity(), (a, b) -> {
            a.setAmount(a.getAmount() + b.getAmount());
            return a;
        }));

        for (Map.Entry<String, InfoPubParam> entry : infoPubParamMap.entrySet()) {
            paramList1.add(entry.getValue());
        }
        System.out.println(paramList1.toString());
    }
}

@Data
class InfoPubParam {
    private String isbn;
    private Long amount;

}

@Data
@ToString
class Sys_testClass implements Serializable {

    private String name;//名称
    private double price;//价格
    private int number;//数量
    private double sum_price;//商品总价格
}
